<template>
  <div id="detail">
    <detail-nav-bar
      class="detail-nav"
      @titleClick="titleClick"
      ref="nav"
    ></detail-nav-bar>

    <scroll
      class="content"
      :probe-type="3"
      :pull-up-load="true"
      ref="scroll"
      @scroll="contentScroll"
    >
      <detail-swiper :topImage="topImages"></detail-swiper>
      <detail-base-info :goods="goods"></detail-base-info>
      <detail-shop-info :shop="Shop"></detail-shop-info>
      <detail-goods-info
        :detailInfo="detailInfo"
        @imageLoad="imageLoad"
      ></detail-goods-info>
      <detail-param-info
        :paramInfo="goodsParam"
        ref="detailParamInfo"
      ></detail-param-info>
    </scroll>

    <detail-bottom-bar @addToCart="addToCart"></detail-bottom-bar>
    <back-top @click.native="backTop" v-show="backTopShow" />
  </div>
</template>

<script>
import detailNavBar from "views/detail/childComs/detailNavBar.vue";
import { getDetail, Goods, Shop, GoodsParam } from "@/network/detail.js";
import DetailSwiper from "./childComs/detailSwiper.vue";
import DetailBaseInfo from "./childComs/DetailBaseInfo.vue";
import Scroll from "components/common/scroll/Scroll.vue";
import DetailShopInfo from "./childComs/DetailShopInfo.vue";
import DetailGoodsInfo from "./childComs/DetailGoodsInfo.vue";
import DetailParamInfo from "./childComs/DetailParamInfo.vue";
import DetailBottomBar from "./childComs/DetailBottomBar.vue";
import BackTop from "@/components/content/backTop/backTop.vue";

export default {
  name: "detail",
  data() {
    return {
      iid: null,
      topImages: [],
      goods: {},
      Shop: {},
      detailInfo: {},
      goodsParam: {},
      themeTopYs: [],
      nowPosition: 0,
      backTopShow: false,
    };
  },
  methods: {
    imageLoad() {
      this.$refs.scroll.scroll.refresh();
      this.$nextTick(() => {
        //等待渲染完成后执行

        this.themeTopYs.push(0);
        this.themeTopYs[1] = this.$refs.detailParamInfo.$el.offsetTop;
      });
    },

    //
    titleClick(index) {
      // console.log(index)
      this.$refs.scroll.scroll.scrollTo(0, -this.themeTopYs[index], 100);
    },

    contentScroll(position) {
      this.nowPosition = -position.y;
      if (this.nowPosition <= this.themeTopYs[1]) {
        this.$refs.nav.currentIndex = 0;
      } else {
        this.$refs.nav.currentIndex = 1;
      }

      if (this.nowPosition > 100) {
        this.backTopShow = true;
      } else {
        this.backTopShow = false;
      }
    },

    //监听滚动位置
    //返回顶部
    backTop() {
      // console.log("组件被点击")
      this.$refs.scroll.scroll.scrollTo(0, 0, 500);
    },
    //添加商品
    addToCart() {
      //获取购物车需要展示的信息
      const product = {};
      product.img = this.topImages[0];
      product.title = this.goods.title;
      product.desc=this.goods.desc;
      product.price=this.goods.newPrice;
      product.iid=this.iid;
      this.$store.commit("addCart",product);
      

    },
  },
  created() {
    this.iid = this.$route.params.iid;
    getDetail(this.iid).then((res) => {
      //   console.log(res);
      //获取顶部图片
      this.topImages = res.result.itemInfo.topImages;
      //   console.log(this.topImages)

      // 获取商品介绍
      const data = res.result;

      this.goods = new Goods(
        data.itemInfo,
        data.columns,
        data.shopInfo.services
      );

      //获取店铺
      this.Shop = new Shop(data.shopInfo);
      console.log(this.goods);

      //商品详情
      this.detailInfo = data.detailInfo;

      //获取参数信息
      this.goodsParam = new GoodsParam(
        data.itemParams.info,
        data.itemParams.rule
      );
    });

    //
  },
  mounted() {},
  components: {
    detailNavBar,
    DetailSwiper,
    DetailBaseInfo,
    scroll: Scroll,
    DetailShopInfo,
    DetailGoodsInfo,
    DetailParamInfo,
    DetailBottomBar,
    BackTop,
  },

  updated() {},
};
</script>

<style scoped>
.content {
  height: calc(100% - 44px - 49px);
}

#detail {
  position: relative;
  z-index: 20;
  background: #fff;
  height: 100vh;
}

.detail-nav {
  position: relative;
  z-index: 25;
  background: #fff;
}
</style>